#include "LedController.h"

LedController::LedController()
{
    // delay(3000); // 3 second delay for boot recovery, and a moment of silence
}

void LedController::Setup()
{
    FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS)
        .setCorrection(TypicalLEDStrip);
    FastLED.setMaxPowerInVoltsAndMilliamps(5, MAX_POWER_MILLIAMPS);
}

LedState LedController::getLedState()
{
    return this->_ledState;
}

void LedController::setLedState(LedState ledState)
{
    this->_isChangeMode = ledState.show_type != this->_ledState.show_type;
    this->_ledState.switch_state = ledState.switch_state;
    this->_ledState.show_type = ledState.show_type;
    this->_ledState.red_data = ledState.red_data;
    this->_ledState.green_data = ledState.green_data;
    this->_ledState.blue_data = ledState.blue_data;
    this->_ledState.bright_data = ledState.bright_data;
}
// setRGB
void LedController::setRGB(CRGB *rgb)
{
    this->_ledState.red_data = rgb->r;
    this->_ledState.green_data = rgb->g;
    this->_ledState.blue_data = rgb->b;
}
// getRGB
CRGB LedController::getRGB()
{
    return CRGB(this->_ledState.red_data, this->_ledState.green_data, this->_ledState.blue_data);
}

uint8_t show_type_2_brightness = 0;
void LedController::show()
{
    bool isChangeMode = this->IsChangeMode();
    if (this->_ledState.switch_state == 0)
    { //关闭
        if (isChangeMode)
        {
            FastLED.clear(true);
        }
        FastLED.showColor(CRGB::Black);
        delay(50);
    }
    else if (this->_ledState.show_type == LedShowType::COMMON)
    {
        if (isChangeMode)
        {
            FastLED.clear(true);
        }
        CRGB color = this->getRGB();
        uint8_t brightness = this->_ledState.bright_data;
        FastLED.showColor(color, brightness);
        delay(50);
    }
    else if (this->_ledState.show_type == LedShowType::DAY)
    {
        if (isChangeMode)
        {
            FastLED.clear(true);
        }
        uint8_t bright = this->_ledState.bright_data;
        if (bright == 0)
            bright = 150;
        FastLED.setBrightness(this->_ledState.bright_data);
        FastLED.showColor(CRGB::White, bright);
        delay(50);
    }
    else if (this->_ledState.show_type == LedShowType::NIGHT)
    {
        if (isChangeMode)
        {
            FastLED.clear(true);
        }
        uint8_t bright = this->_ledState.bright_data;
        if (bright == 0)
            bright = 150;
        FastLED.showColor(CRGB::Orange, bright);
        delay(50);
    }
    else if (this->_ledState.show_type == LedShowType::COLOR)
    {
        if (isChangeMode)
        {
            FastLED.clear(true);
        }
        CRGB colors[] = {CRGB::Red, CRGB::Yellow, CRGB::Blue, CRGB::Green};
        int len = sizeof(colors) / sizeof(colors[0]);
        for (int i = 0; i < NUM_LEDS; i++)
        {
            int index = i % len;
            leds[i] = colors[index];
        }
        uint8_t bright = this->_ledState.bright_data;
        if (bright == 0)
            bright = 150;
        FastLED.setBrightness(bright);
        FastLED.show();
        delay(50);
    }
    else if (this->_ledState.show_type == LedShowType::ColorPalette)
    {
        if (isChangeMode)
        {
            FastLED.clear(true);
            ledShow_ColorPalette.ShowSetup(this->getLedState());
        }
        ledShow_ColorPalette.LoopShow();
    }
    else if (this->_ledState.show_type == LedShowType::ColorTemperature1)
    {
        if (isChangeMode)
        {
            FastLED.clear(true);
            ledShow_ColorTemperature.ShowSetup(this->getLedState());
        }
        ledShow_ColorTemperature.LoopShow();
    }
    else if (this->_ledState.show_type == LedShowType::Cylon)
    {
        if (isChangeMode)
        {
            FastLED.clear(true);
            ledShow_Cylon.ShowSetup(this->getLedState());
        }
        ledShow_Cylon.LoopShow();
    }
}

bool LedController::IsChangeMode()
{
    bool result = this->_isChangeMode;
    if (this->_isChangeMode)
        this->_isChangeMode = false;
    return result;
}